package com.bjwl.service.safty.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.bjwl.base.PageParams;
import com.bjwl.dao.safty.RoleDao;
import com.bjwl.model.Module;
import com.bjwl.model.Role;
import com.bjwl.service.safty.RoleService;
import com.bjwl.view.MenuView;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class RoleServiceImpl implements RoleService{

	@Autowired
	private RoleDao roleDao;
	@Override
	public PageInfo<Role> queryRole(PageParams pageParams) {
		PageHelper.startPage(pageParams);
		List<Role> roles=roleDao.queryRole();
		//创建PageInfo对象，将list集合放进去
		PageInfo<Role> pageInfo=new PageInfo<>(roles);
		return pageInfo;
	}
	@Override
	public boolean enableRole(Integer ro_id) {
		int flag=roleDao.enableRole(ro_id);
		if(flag>0) {
			return true;
		}
		return false;
	}
	@Override
	public boolean disableRole(Integer ro_id) {
		int flag=roleDao.disableRole(ro_id);
		if(flag>0) {
			return true;
		}
		return false;
	}
	@Override
	public boolean addRole(Role role) {
		
		int flag=roleDao.addRole(role);
		if(flag>0) {
			return true;
		}
		return false;
	}
	@Override
	public boolean editRole(Role role) {
		int flag=roleDao.editRole(role);
		if(flag>0) {
			return true;
		}
		return false;
	}
	@Override
	public List<MenuView> queryModuleAll() {
		List<Module> moduleList=roleDao.queryModuleAll();
		//所有菜单
		List<MenuView> menuList=new ArrayList<MenuView>();
		
		//主菜单
		MenuView mainMenu=null;
		for(Module module:moduleList) {
			//当主菜单为空或者主菜单的id不等于当前module的父id
			if(mainMenu==null||!mainMenu.getM_id().equals(module.getP_id())) {
				System.out.println(module.getM_id());
				//封装主菜单
				mainMenu=new MenuView();
				mainMenu.setM_id(module.getP_id());
				mainMenu.setM_name(module.getP_name());
				mainMenu.setSubMenuViews(new ArrayList<MenuView>());
				//将此主菜单添加到所有菜单列表中
				menuList.add(mainMenu);
				
			}
			
			//封装子菜单
			MenuView subMenu=new MenuView();
			subMenu.setM_id(module.getM_id());
			subMenu.setM_name(module.getM_name());
			subMenu.setM_url(module.getM_url());
			//将子菜单添加到主菜单中
			mainMenu.getSubMenuViews().add(subMenu);
		}
		return menuList;
	}
	@Override
	public List<Integer> queryAllRoleById(Integer ro_id) {
		List<Integer> moduleIdList=roleDao.queryAllRoleById(ro_id);
		return moduleIdList;
	}
	//修改用户权限的业务层
	@Override
	public Boolean updateRoleRight(Integer ro_id, List<Integer> roleIdList) {
		//int num=roleIdList.getClass().getDeclaredFields().length;
		int num=roleIdList.size();
		System.out.println("num:"+num);
		Boolean bo=true;
//		int[] add=new int[num];
		try {
			//先根据id删除相应权限
			if(roleDao.deleteRightByRoleId(ro_id)<0) {
				return false;
			}
			//，再根据id添加权限
			//获取权限值
			for(int i=0;i<num;i++) {
				//Integer m_id=roleIdList.getIntValue(i);
				Integer m_id=roleIdList.get(i);
				if(roleDao.addRightByRoleId(ro_id,m_id)<0) {
					return false;
				}
			}
		} catch (SecurityException e) {
			
			e.printStackTrace();
		}
		
		return bo;
	}

	

}
